﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars.Docking2010;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;


namespace ERPClient
{
    public partial class InventoryForm : Form
    {
        public DocumentManager docuManager { set; get; }
        public InventoryForm()
        {
            InitializeComponent();
            CursorCreator.Init();
            gridControl1.AllowDrop = true;
            gridView1.OptionsBehavior.Editable = false;


            //不自動調整欄寬
            gridView1.OptionsView.ColumnAutoWidth = false;
            gridView1.ScrollStyle = ScrollStyleFlags.LiveHorzScroll | ScrollStyleFlags.LiveVertScroll;
            gridView1.HorzScrollVisibility = ScrollVisibility.Always;
        }

        private void InventoryForm_Load(object sender, EventArgs e)
        {
            // TODO: 這行程式碼會將資料載入 'astoolDataSet1.Inventory' 資料表。您可以視需要進行移動或移除。
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            this.inventoryTableAdapter.FillByID(this.astoolDataSet1.Inventory, textEdit1.Text.ToString());
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            if (textEdit1.Text != "")
                this.inventoryTableAdapter.FillByBomID(this.astoolDataSet1.Inventory, Int32.Parse(textEdit1.Text));
        }


        GridHitInfo downHitInfo = null;
        GridView source;
        System.Drawing.Point sourceCell;
        private void gridView1_MouseDown(object sender, MouseEventArgs e)
        {
            GridView view = sender as GridView;
            downHitInfo = null;
            GridHitInfo hitInfo = view.CalcHitInfo(new Point(e.X, e.Y));
            if (Control.ModifierKeys != Keys.None) return;
            if (hitInfo.Column == null) return;

            GridCell[] cells = view.GetSelectedCells();

            if (e.Button == MouseButtons.Left && hitInfo.RowHandle >= 0)
            {
                downHitInfo = hitInfo;
                source = view;
                sourceCell = new System.Drawing.Point(downHitInfo.Column.VisibleIndex, downHitInfo.RowHandle);
            }
        }

        private void gridView1_MouseMove(object sender, MouseEventArgs e)
        {
            GridView view = sender as GridView;
            if (e.Button == MouseButtons.Left && downHitInfo != null)
            {
                Size dragSize = SystemInformation.DragSize;
                Rectangle dragRect = new Rectangle(new Point(downHitInfo.HitPoint.X - dragSize.Width / 2,
                    downHitInfo.HitPoint.Y - dragSize.Height / 2), dragSize);

                if (!dragRect.Contains(new Point(e.X, e.Y)))
                {
                    object data = view.GetRowCellValue(downHitInfo.RowHandle, downHitInfo.Column);
                    view.GridControl.DoDragDrop(data, DragDropEffects.Move);
                    downHitInfo = null;
                    DevExpress.Utils.DXMouseEventArgs.GetMouseArgs(e).Handled = true;
                }
            }
        }

        private void gridControl1_DragDrop(object sender, DragEventArgs e)
        {
            GridControl grid = sender as GridControl;
            DataTable table = grid.DataSource as DataTable;
            object value = e.Data.GetData(typeof(DateTime));
            GridView view = grid.MainView as GridView;
            GridHitInfo hitInfo = view.CalcHitInfo(grid.PointToClient(new Point(e.X, e.Y)));
            view.SetRowCellValue(hitInfo.RowHandle, hitInfo.Column, value);
            if (source == view) source.SetRowCellValue(sourceCell.Y, source.Columns[sourceCell.X], null);
        }

        private void gridControl1_DragOver(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(DateTime)))
                e.Effect = DragDropEffects.Move;
            else
                e.Effect = DragDropEffects.None;
        }

        private void gridControl1_GiveFeedback(object sender, GiveFeedbackEventArgs e)
        {
            if (downHitInfo != null)
            {
                e.UseDefaultCursors = false;
                Cursor.Current = CursorCreator.Cursor1();
            }
        }       
    }
}
